using System;
using System.Collections;
using System.Data;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
namespace BMC.ARSystem
{
	[Guid("0fd26714-e2b6-4ca4-82a4-c17c1d246a1c")]
	public class Util
	{
		private class MyMatchEval
		{
			public string QuoteKeywords(Match m)
			{
				return Util.QuoteString(m.get_Value());
			}
		}
		public static object GetAt(object rawData, int[] indexSpec)
		{
			int i = 0;
			int num = indexSpec.Length;
			while (i < num)
			{
				rawData = ((object[])rawData)[indexSpec[i]];
				i++;
			}
			return rawData;
		}
		public static ArrayList BuildGLEResponse(object rawData, string name)
		{
			ArrayList arrayList = new ArrayList();
			object[] array = rawData as object[];
			if (array != null)
			{
				int i = 0;
				int num = array.Length;
				while (i < num)
				{
					object[] array2 = array[i] as object[];
					if (array2 != null)
					{
						EntryDescription entryDescription = new EntryDescription(array2, null);
						arrayList.Add(entryDescription);
					}
					i++;
				}
			}
			return arrayList;
		}
		public static EntryFieldValueList BuildGLEWFResponse(object rawData)
		{
			EntryFieldValueList entryFieldValueList = new EntryFieldValueList();
			object[] array = rawData as object[];
			if (array != null)
			{
				int i = 0;
				int num = array.Length;
				while (i < num)
				{
					object[] array2 = array[i] as object[];
					if (array2 != null)
					{
						string entryId = Util.EntryIdToString(array2[0]);
						ARTypeMgr arg_70_0 = ARTypeMgr.Instance;
						Type arg_70_1 = typeof(FieldValueList);
						string arg_70_2 = "System.UInt32";
						int[] keyIndex = new int[1];
						FieldValueList fvl = (FieldValueList)arg_70_0.BuildCustomDictionaryFromVariant(arg_70_1, arg_70_2, keyIndex, "ARValue", new int[]
						{
							1
						}, (object[])array2[1], null);
						entryFieldValueList.Add(new EntryFieldValue(entryId, fvl));
					}
					i++;
				}
			}
			return entryFieldValueList;
		}
		public static DataTable BuildGLEWFResponseDataTable(object rawData, string name, string[] columnNames, Type[] colTypes)
		{
			DataTable dataTable = new DataTable(name);
			int num = colTypes.Length;
			for (int i = 0; i < num; i++)
			{
				DataColumn dataColumn = new DataColumn();
				DataColumn arg_26_0 = dataColumn;
				string columnName;
				dataColumn.set_Caption(columnName = columnNames[i]);
				arg_26_0.set_ColumnName(columnName);
				dataColumn.set_DataType(colTypes[i]);
				dataTable.get_Columns().Add(dataColumn);
			}
			dataTable.set_PrimaryKey(new DataColumn[]
			{
				dataTable.get_Columns().get_Item(columnNames[0])
			});
			object[] array = rawData as object[];
			if (array != null)
			{
				int j = 0;
				int num2 = array.Length;
				while (j < num2)
				{
					object[] array2 = array[j] as object[];
					if (array2 != null)
					{
						DataRow dataRow = dataTable.NewRow();
						int num3 = 0;
						dataRow.set_Item(num3++, Util.EntryIdToString(array2[0]));
						for (int k = 0; k < num - 1; k++)
						{
							object[] array3 = (object[])Util.GetAt(array2, new int[]
							{
								1,
								k,
								1
							});
							if (array3 != null)
							{
								dataRow.set_Item(num3++, Util.ConvertFromARValue(array3, false, false));
							}
						}
						dataTable.get_Rows().Add(dataRow);
					}
					j++;
				}
			}
			return dataTable;
		}
		public static DataTable BuildGEResponseDataTable(object rawData, string name, string[] columnNames, Type[] colTypes)
		{
			DataTable dataTable = new DataTable(name);
			int num = colTypes.Length;
			for (int i = 0; i < num; i++)
			{
				DataColumn dataColumn = new DataColumn();
				DataColumn arg_26_0 = dataColumn;
				string columnName;
				dataColumn.set_Caption(columnName = columnNames[i]);
				arg_26_0.set_ColumnName(columnName);
				dataColumn.set_DataType(colTypes[i]);
				dataTable.get_Columns().Add(dataColumn);
			}
			dataTable.set_PrimaryKey(new DataColumn[]
			{
				dataTable.get_Columns().get_Item(columnNames[0])
			});
			object[] array = rawData as object[];
			if (array != null)
			{
				int j = 0;
				int num2 = array.Length;
				while (j < num2)
				{
					object[] array2 = array[j] as object[];
					if (array2 != null)
					{
						DataRow dataRow = dataTable.NewRow();
						int num3 = 0;
						dataRow.set_Item(num3++, Util.EntryIdToString(array2[0]));
						for (j = 0; j < num - 1; j++)
						{
							object[] array3 = (object[])Util.GetAt(array2, new int[]
							{
								1,
								j,
								1
							});
							if (array3 != null)
							{
								dataRow.set_Item(num3++, Util.ConvertFromARValue(array3, false, false));
							}
						}
						dataTable.get_Rows().Add(dataRow);
					}
					j++;
				}
			}
			return dataTable;
		}
		public static DataTable BuildGLSQLResponseDataTable(object[] rawData, Type[] colTypes)
		{
			DataTable dataTable = new DataTable();
			bool flag = false;
			if (colTypes != null)
			{
				for (int i = 0; i < colTypes.Length; i++)
				{
					Type dataType = colTypes[i];
					DataColumn dataColumn = new DataColumn();
					dataColumn.set_DataType(dataType);
					dataColumn.set_ReadOnly(true);
					dataColumn.set_Unique(false);
					dataTable.get_Columns().Add(dataColumn);
				}
				flag = true;
			}
			if (rawData != null)
			{
				int j = 0;
				int num = rawData.Length;
				while (j < num)
				{
					object[] array = rawData[j] as object[];
					if (array != null)
					{
						object[] array2;
						if (!flag)
						{
							array2 = array;
							for (int i = 0; i < array2.Length; i++)
							{
								object obj = array2[i];
								object[] array3 = obj as object[];
								if (array3 != null)
								{
									Util.ConvertFromARValue(array3, false, false);
								}
								DataColumn dataColumn = new DataColumn();
								dataColumn.set_DataType(typeof(object));
								dataColumn.set_ReadOnly(true);
								dataColumn.set_Unique(false);
								dataTable.get_Columns().Add(dataColumn);
							}
							flag = true;
						}
						DataRow dataRow = dataTable.NewRow();
						int num2 = 0;
						array2 = array;
						for (int i = 0; i < array2.Length; i++)
						{
							object obj2 = array2[i];
							DataColumn dataColumn = dataTable.get_Columns().get_Item(num2);
							object[] array4 = obj2 as object[];
							if (array4 != null)
							{
								object obj3 = Util.ConvertFromARValue(array4, false, false);
								if (dataColumn.get_DataType().Equals(typeof(DateTime)))
								{
									dataRow.set_Item(num2, Util.GetDateTime(Convert.ToUInt32(obj3)));
								}
								else
								{
									dataRow.set_Item(num2, obj3);
								}
								num2++;
							}
						}
						dataTable.get_Rows().Add(dataRow);
					}
					j++;
				}
			}
			return dataTable;
		}
		public static object StringToEntryId(string entryId)
		{
			string[] given = entryId.Split(new char[]
			{
				'|'
			});
			return ARTypeMgr.Instance.toVariant(given);
		}
		public static string EntryIdToString(object rawData)
		{
			StringBuilder stringBuilder = new StringBuilder();
			object[] array = rawData as object[];
			if (array != null)
			{
				int i = 0;
				int num = array.Length;
				while (i < num)
				{
					stringBuilder.AppendFormat("{0}|", array[i].ToString());
					i++;
				}
			}
			string text = stringBuilder.ToString();
			return text.TrimEnd("|".ToCharArray());
		}
		public static int GetARTimestamp(DateTime date)
		{
			return (int)(date - new DateTime(1970, 1, 1, 0, 0, 0)).get_TotalSeconds();
		}
		public static DateTime GetDateTime(uint unixTime)
		{
			DateTime result = new DateTime(1970, 1, 1, 0, 0, 0);
			result = result.AddSeconds(unixTime);
			return result;
		}
		public static object ConvertFromARValue(object[] arValue, bool isStatusHistory, bool preserveEnumTypeInfo)
		{
			if (arValue == null && arValue.Length < 2)
			{
				return DBNull.Value;
			}
			int aRValueType = Util.GetARValueType(arValue);
			int num = aRValueType;
			switch (num)
			{
			case 0:
				return DBNull.Value;
			case 1:
				return string.Format("$-{0}$", Convert.ToInt32(arValue[1]));
			case 2:
				return Convert.ToInt32(arValue[1]);
			case 3:
				return Convert.ToDouble(arValue[1]);
			case 4:
				if (isStatusHistory)
				{
					return StatusHistory.Decode(arValue[1].ToString().Trim());
				}
				return arValue[1].ToString();
			case 5:
				try
				{
					DiaryList diaryList = DiaryList.Decode(arValue[1].ToString());
					object result = diaryList;
					return result;
				}
				catch (Exception var_2_EE)
				{
					object result = arValue[1];
					return result;
				}
				break;
			case 6:
				break;
			case 7:
				if (arValue[1] is DateTime)
				{
					return arValue[1];
				}
				return Util.GetDateTime(Convert.ToUInt32(arValue[1]));
			case 8:
				return arValue[1];
			case 9:
				return ARTypeMgr.Instance.ObjectFromVariant(typeof(ByteList), arValue[1], null);
			case 10:
				return Util.ToDecimal(arValue[1]);
			case 11:
				if (arValue[1] is Attachment)
				{
					return arValue[1];
				}
				return ARTypeMgr.Instance.ObjectFromVariant(typeof(Attachment), arValue[1], null);
			case 12:
				if (arValue[1] is CurrencyValue)
				{
					return arValue[1];
				}
				return ARTypeMgr.Instance.ObjectFromVariant(typeof(CurrencyValue), arValue[1], null);
			case 13:
				if (arValue[1] is DateValue)
				{
					return arValue[1] as DateValue;
				}
				if (arValue[1] is DateTime)
				{
					DateTime dateTime = (DateTime)arValue[1];
					int arg_204_0 = dateTime.get_Year();
					dateTime = (DateTime)arValue[1];
					int arg_204_1 = dateTime.get_Month();
					dateTime = (DateTime)arValue[1];
					return new DateValue(arg_204_0, arg_204_1, dateTime.get_Day());
				}
				return new DateValue(Convert.ToInt32(arValue[1]));
			case 14:
				if (arValue[1] is TimeOfDayValue)
				{
					return arValue[1] as TimeOfDayValue;
				}
				if (arValue[1] is DateTime)
				{
					return arValue[1];
				}
				return new TimeOfDayValue(Convert.ToInt32(arValue[1]));
			default:
				if (num == 33)
				{
					return arValue;
				}
				switch (num)
				{
				case 40:
					return (uint)arValue[1];
				case 41:
					return ARTypeMgr.Instance.ArrayListFromVariant("BMC.ARSystem.Coordinate", (object[])arValue[1], null);
				case 42:
					return arValue;
				default:
					return DBNull.Value;
				}
				break;
			}
			if (preserveEnumTypeInfo)
			{
				return arValue;
			}
			return Convert.ToInt32(arValue[1]);
		}
		public static bool IsKeyword(string str, out int keyword)
		{
			keyword = 42;
			if (str.StartsWith("$-") && str.EndsWith("$"))
			{
				string text = str.Substring(2, str.get_Length() - 3);
				try
				{
					keyword = Convert.ToInt32(text);
				}
				catch (Exception)
				{
					IDictionary keywords = ARTypeMgr.Instance.Keywords;
					ArrayList arrayList = (ArrayList)keywords.get_Item(text);
					if (arrayList != null)
					{
						keyword = (int)arrayList.get_Item(1);
					}
				}
			}
			return keyword != 42;
		}
		public static void FixARDisplayProps(string arTypStr, object obj)
		{
			if (obj == null)
			{
				return;
			}
			if (arTypStr.Equals("Prop"))
			{
				object[] array = obj as object[];
				object obj2 = array[0];
				if (Convert.ToInt32(((object[])array[1])[0]) == 1)
				{
					return;
				}
				IDictionary displayProperties = ARTypeMgr.Instance.DisplayProperties;
				ArrayList arrayList = (ArrayList)displayProperties.get_Item(obj2.ToString());
				if (arrayList != null)
				{
					string text = (string)arrayList.get_Item(2);
					if (text.Equals("ENUM"))
					{
						((object[])array[1])[0] = 6;
						return;
					}
					if (text.Equals("BITMASK"))
					{
						((object[])array[1])[0] = 8;
						return;
					}
					if (text.Equals("ULONG"))
					{
						((object[])array[1])[0] = 40;
						return;
					}
				}
			}
			else
			{
				if (arTypStr.Equals("PropList"))
				{
					object[] array2 = obj as object[];
					object[] array3 = array2;
					for (int i = 0; i < array3.Length; i++)
					{
						object obj3 = array3[i];
						Util.FixARDisplayProps("Prop", obj3);
					}
					return;
				}
				if (arTypStr.Equals("DisplayInstanceList"))
				{
					object[] array4 = obj as object[];
					object[] array3 = array4;
					for (int i = 0; i < array3.Length; i++)
					{
						object obj4 = array3[i];
						Util.FixARDisplayProps("PropList", ((object[])obj4)[1]);
					}
				}
			}
		}
		public static object ConvertToARValue(object val)
		{
			object[] array = new object[2];
			if (val is Array && ((object[])val).Length == 2)
			{
				Array.Copy((Array)val, 0, array, 0, 2);
				if (array[1] is DateTime)
				{
					array[1] = Util.GetARTimestamp((DateTime)array[1]);
				}
				else
				{
					if (array[1] is decimal)
					{
						array[1] = array[1].ToString();
					}
					else
					{
						if (array[1] is Attachment)
						{
							array[0] = 11;
							object obj = ARTypeMgr.Instance.toVariant(array[1]);
							byte[] content = ((Attachment)array[1]).Content;
							((object[])obj)[3] = new object[]
							{
								2,
								new object[]
								{
									content.Length,
									content
								}
							};
							array[1] = obj;
						}
						else
						{
							if (array[1] is CurrencyValue)
							{
								array[1] = ARTypeMgr.Instance.toVariant(array[1]);
							}
							else
							{
								if (array[1] is DiaryList)
								{
									array[1] = ((DiaryList)array[1]).Encode();
								}
								else
								{
									if (array[1] is StatusHistory)
									{
										array[0] = 4;
										array[1] = ((StatusHistory)array[1]).Encode();
									}
									else
									{
										if (array[1] is ByteList)
										{
											array[1] = ARTypeMgr.Instance.toVariant(array[1]);
										}
									}
								}
							}
						}
					}
				}
			}
			else
			{
				if (val is int)
				{
					array[0] = 2;
					array[1] = val;
				}
				else
				{
					if (val is string)
					{
						int num;
						if (Util.IsKeyword((string)val, out num))
						{
							array[0] = 1;
							array[1] = num;
						}
						else
						{
							array[0] = 4;
							array[1] = val;
						}
					}
					else
					{
						if (val is double)
						{
							array[0] = 3;
							array[1] = val;
						}
						else
						{
							if (val is DateTime)
							{
								array[0] = 7;
								array[1] = Util.GetARTimestamp((DateTime)val);
							}
							else
							{
								if (val is DateValue)
								{
									array[0] = 13;
									array[1] = ((DateValue)val).JulianDate;
								}
								else
								{
									if (val is TimeOfDayValue)
									{
										array[0] = 14;
										array[1] = ((TimeOfDayValue)val).TotalSeconds;
									}
									else
									{
										if (val is decimal)
										{
											array[0] = 10;
											array[1] = val.ToString();
										}
										else
										{
											if (val is Attachment)
											{
												array[0] = 11;
												array[1] = ARTypeMgr.Instance.toVariant(val);
												byte[] content2 = ((Attachment)val).Content;
												((object[])array[1])[3] = new object[]
												{
													2,
													new object[]
													{
														content2.Length,
														content2
													}
												};
											}
											else
											{
												if (val is CurrencyValue)
												{
													array[0] = 12;
													array[1] = ARTypeMgr.Instance.toVariant(val);
												}
												else
												{
													if (val is DiaryList)
													{
														array[0] = 5;
														array[1] = ((DiaryList)val).Encode();
													}
													else
													{
														if (val is StatusHistory)
														{
															array[0] = 4;
															array[1] = ((StatusHistory)val).Encode();
														}
														else
														{
															if (val is ByteList)
															{
																array[0] = 9;
																array[1] = ARTypeMgr.Instance.toVariant(val);
															}
															else
															{
																if (val is Array && ((Array)val).get_Length() > 0 && ((object[])val)[0] is byte)
																{
																	array[0] = 9;
																	array[1] = val;
																}
																else
																{
																	if (val is uint)
																	{
																		array[0] = 40;
																		array[1] = val;
																	}
																	else
																	{
																		if (val is ArrayList && ((ArrayList)val).get_Count() > 0 && ((ArrayList)val).get_Item(0) is Coordinate)
																		{
																			array[0] = 41;
																			array[1] = ARTypeMgr.Instance.toVariant(val);
																		}
																		else
																		{
																			array[0] = 0;
																			array[1] = DBNull.Value;
																		}
																	}
																}
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			return array;
		}
		public static int GetARValueType(object val)
		{
			int result;
			if (val is Array && ((object[])val).Length == 2)
			{
				result = Convert.ToInt32(((object[])val)[0]);
			}
			else
			{
				if (val is int)
				{
					result = 2;
				}
				else
				{
					if (val is string)
					{
						int num;
						if (Util.IsKeyword((string)val, out num))
						{
							result = 1;
						}
						else
						{
							result = 4;
						}
					}
					else
					{
						if (val is double)
						{
							result = 3;
						}
						else
						{
							if (val is DateTime)
							{
								result = 7;
							}
							else
							{
								if (val is decimal)
								{
									result = 10;
								}
								else
								{
									if (val is uint)
									{
										result = 40;
									}
									else
									{
										if (val is Attachment)
										{
											result = 11;
										}
										else
										{
											if (val is ArrayList && ((ArrayList)val).get_Item(0) is Coordinate)
											{
												result = 41;
											}
											else
											{
												if (val is CurrencyValue)
												{
													result = 12;
												}
												else
												{
													if (val is DiaryList)
													{
														result = 5;
													}
													else
													{
														if (val is Array && ((object[])val)[0] is byte)
														{
															result = 9;
														}
														else
														{
															result = 0;
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
			return result;
		}
		public static string QuoteString(string str)
		{
			return string.Format("\"{0}\"", str);
		}
		public static string SpecialCommandParamEncoding(string str)
		{
			str = str.Replace("\"", "\"\"");
			str = Util.QuoteString(str);
			Regex regex = new Regex("\\$[\\w,\\s]*\\$");
			MatchEvaluator matchEvaluator = new MatchEvaluator(new Util.MyMatchEval().QuoteKeywords);
			str = regex.Replace(str, matchEvaluator);
			return str;
		}
		public static ArrayList BuildExpMenuResponse(object rawData)
		{
			ArrayList arrayList = new ArrayList();
			if (rawData is Array && ((object[])rawData)[0] is uint)
			{
				uint num = (uint)((object[])rawData)[0];
				if (num == 1u)
				{
					object[] array = (object[])((object[])rawData)[1];
					object[] array2 = array;
					for (int i = 0; i < array2.Length; i++)
					{
						object obj = array2[i];
						if ((uint)((object[])obj)[1] == 1)
						{
							arrayList.Add(new MenuItem((string)((object[])obj)[0], (string)((object[])obj)[2]));
						}
						else
						{
							arrayList.Add(new MenuItem((string)((object[])obj)[0], Util.BuildExpMenuResponse(((object[])obj)[2])));
						}
					}
				}
			}
			return arrayList;
		}
		public static Menu BuildMenuInstance(object[] rawData, Server server)
		{
			switch (Convert.ToUInt32(rawData[0]))
			{
			case 1u:
				return new ListMenu(rawData, server);
			case 2u:
				return new QueryMenu((object[])rawData[1], server);
			case 3u:
				return (Menu)ARTypeMgr.Instance.ObjectFromVariant(typeof(FileMenu), rawData[1], server);
			case 4u:
				return new SqlMenu((object[])rawData[1], server);
			case 6u:
				switch (Convert.ToUInt32(Util.GetAt(rawData, new int[]
				{
					1,
					3
				})))
				{
				case 1u:
					return new FormDataDictionaryMenu((object[])rawData[1], server);
				case 2u:
					return new FieldDataDictionaryMenu((object[])rawData[1], server);
				}
				break;
			}
			return null;
		}
		public static ArrayList BuildEntryStatResponse(object rawData)
		{
			ArrayList arrayList = new ArrayList();
			object[] array = (object[])rawData;
			for (int i = 0; i < array.Length; i++)
			{
				object[] array2 = (object[])array[i];
				object val = Util.ConvertFromARValue((object[])array2[1], false, false);
				ArrayList groupByValues = ARTypeMgr.Instance.ArrayListFromVariant("ARValue", (object[])array2[0], null);
				arrayList.Add(new StatisticResult(val, groupByValues));
			}
			return arrayList;
		}
		public static ArrayList BuildGLCOResponse(object rawData)
		{
			ArrayList arrayList = new ArrayList();
			object[] array = (object[])rawData;
			for (int i = 0; i < array.Length; i++)
			{
				object[] array2 = (object[])array[i];
				object obj = (string)array2[0];
				arrayList.Add(obj);
			}
			return arrayList;
		}
		public static int[] SplitIntoIntArray(string str)
		{
			int[] array = new int[0];
			if (str != "")
			{
				string[] array2 = str.Split(new char[]
				{
					';'
				});
				string[] array3 = new string[array2.Length];
				int num = 0;
				string[] array4 = array2;
				for (int i = 0; i < array4.Length; i++)
				{
					string text = array4[i];
					if (text.get_Length() > 0)
					{
						array3[num++] = text;
					}
				}
				if (num != array2.Length)
				{
					string[] array5 = new string[num];
					Array.Copy(array3, 0, array5, 0, num);
					array3 = array5;
				}
				array = new int[array3.Length];
				int num2 = 0;
				array4 = array3;
				for (int i = 0; i < array4.Length; i++)
				{
					string text2 = array4[i];
					if (text2.get_Length() != 0)
					{
						array[num2++] = Convert.ToInt32(text2);
					}
				}
			}
			return array;
		}
		public static string CollectionToString(ICollection ids)
		{
			StringBuilder stringBuilder = new StringBuilder();
			IEnumerator enumerator = ids.GetEnumerator();
			int i = 0;
			int count = ids.get_Count();
			while (i < count)
			{
				stringBuilder.Append(string.Format("{0};", enumerator.get_Current()));
				enumerator.MoveNext();
				i++;
			}
			return stringBuilder.ToString();
		}
		public static ArrayList BuildFieldAssignmentList(object rawData, Server server, string localForm, string remoteForm, out bool isByMatchingIds, out bool isSetToDefaults)
		{
			isByMatchingIds = (isSetToDefaults = false);
			ArrayList arrayList = new ArrayList();
			object[] array = rawData as object[];
			if (array != null && array.Length > 0)
			{
				object[] array2 = array;
				for (int i = 0; i < array2.Length; i++)
				{
					object obj = array2[i];
					object arg_49_0 = (object[])obj;
					int[] array3 = new int[1];
					uint num = Convert.ToUInt32(Util.GetAt(arg_49_0, array3));
					if ((ulong)num == 98uL)
					{
						isByMatchingIds = true;
						break;
					}
					if ((ulong)num == 97uL)
					{
						isSetToDefaults = true;
						break;
					}
					object[] assignment = (object[])Util.GetAt((object[])obj, new int[]
					{
						1
					});
					object obj2 = "";
					object arg_B1_0 = (object[])obj;
					array3 = new int[2];
					array3[0] = 1;
					uint num2 = (uint)Util.GetAt(arg_B1_0, array3);
					switch (num2)
					{
					case 1u:
						obj2 = Util.ConvertFromARValue((object[])Util.GetAt((object[])obj, new int[]
						{
							1,
							1
						}), false, true);
						break;
					case 2u:
					case 4u:
					case 5u:
						obj2 = server.EncodeAssignment(assignment);
						obj2 = server.FormatAssignment(localForm, num, remoteForm, obj2);
						break;
					case 3u:
						obj2 = string.Format("$PROCESS$ {0}", (string)Util.GetAt((object[])obj, new int[]
						{
							1,
							1
						}));
						break;
					case 6u:
					{
						object arg_173_0 = (object[])obj;
						array3 = new int[3];
						array3[0] = 1;
						array3[1] = 1;
						string text = (string)Util.GetAt(arg_173_0, array3);
						string text2 = (string)Util.GetAt((object[])obj, new int[]
						{
							1,
							1,
							1
						});
						string text3 = (string)Util.GetAt((object[])obj, new int[]
						{
							1,
							1,
							2
						});
						string text4 = (string)Util.GetAt((object[])obj, new int[]
						{
							1,
							1,
							4
						});
						obj2 = string.Format("$DDE$ {0};{1};{2}{3}{4}", new object[]
						{
							text,
							text2,
							text4,
							(text3.get_Length() > 0) ? ";" : "",
							text3
						});
						break;
					}
					case 7u:
						obj2 = "$" + Util.GetAt(obj, new int[]
						{
							1,
							1,
							2
						}).ToString() + "$";
						break;
					case 8u:
						obj2 = "$" + Util.GetAt(obj, new int[]
						{
							1,
							1,
							2
						}).ToString() + "$";
						break;
					}
					if (obj2 == DBNull.Value)
					{
						obj2 = Keyword.Null;
					}
					arrayList.Add(new object[]
					{
						num,
						obj2,
						num2
					});
				}
			}
			return arrayList;
		}
		public static ArrayList BuildAssignmentList(object rawData, Server server, string localForm, string remoteForm)
		{
			ArrayList arrayList = new ArrayList();
			object[] array = rawData as object[];
			if (array != null && array.Length > 0)
			{
				object[] array2 = array;
				for (int i = 0; i < array2.Length; i++)
				{
					object obj = array2[i];
					object[] array3 = obj as object[];
					object obj2 = "";
					object arg_46_0 = array3;
					int[] array4 = new int[1];
					switch ((uint)Util.GetAt(arg_46_0, array4))
					{
					case 1u:
						obj2 = Util.ConvertFromARValue((object[])Util.GetAt(array3, new int[]
						{
							1
						}), false, true);
						break;
					case 2u:
					case 4u:
					case 5u:
						obj2 = server.EncodeAssignment(array3);
						obj2 = server.FormatAssignment(localForm, server.Session.CurrentFieldId, remoteForm, obj2);
						break;
					case 3u:
						obj2 = string.Format("$PROCESS$ {0}", (string)Util.GetAt(array3, new int[]
						{
							1
						}));
						break;
					case 6u:
					{
						object arg_EA_0 = array3;
						array4 = new int[2];
						array4[0] = 1;
						string text = (string)Util.GetAt(arg_EA_0, array4);
						string text2 = (string)Util.GetAt(array3, new int[]
						{
							1,
							1
						});
						string text3 = (string)Util.GetAt(array3, new int[]
						{
							1,
							2
						});
						string text4 = (string)Util.GetAt(array3, new int[]
						{
							1,
							4
						});
						obj2 = string.Format("$DDE$ {0};{1};{2}{3}{4}", new object[]
						{
							text,
							text2,
							text4,
							(text3.get_Length() > 0) ? ";" : "",
							text3
						});
						break;
					}
					case 7u:
						obj2 = "$" + Util.GetAt(array3, new int[]
						{
							1,
							2
						}).ToString() + "$";
						break;
					case 8u:
						obj2 = "$" + Util.GetAt(array3, new int[]
						{
							1,
							2
						}).ToString() + "$";
						break;
					}
					arrayList.Add(obj2);
				}
			}
			return arrayList;
		}
		private static string getNextToken(string str, out string remainingStr)
		{
			int num = str.IndexOf("\\");
			string result;
			if (num >= 0)
			{
				result = str.Substring(0, num);
				remainingStr = str.Substring(num + 1);
			}
			else
			{
				result = str;
				remainingStr = null;
			}
			return result;
		}
		public static Hashtable ImportOpenWindowSampleServerForms(string encoded)
		{
			try
			{
				string nextToken = Util.getNextToken(encoded, out encoded);
				int num = Convert.ToInt32(nextToken);
				Hashtable hashtable = new Hashtable(num);
				for (int i = 0; i < num; i++)
				{
					nextToken = Util.getNextToken(encoded, out encoded);
					int num2 = Convert.ToInt32(nextToken);
					string nextToken2 = Util.getNextToken(encoded, out encoded);
					string nextToken3 = Util.getNextToken(encoded, out encoded);
					hashtable.set_Item(num2, new object[]
					{
						nextToken2,
						nextToken3
					});
				}
				return hashtable;
			}
			catch
			{
			}
			return null;
		}
		public static object GetARStructItemList(object[,] items, bool useXml)
		{
			object[] array = new object[items.GetUpperBound(0) - items.GetLowerBound(0) + 1];
			for (int i = items.GetLowerBound(0); i <= items.GetUpperBound(0); i++)
			{
				object[] array2 = new object[3];
				array2[0] = ((int)items[i, 0] | (useXml ? 1073741824 : 0));
				array2[1] = (string)items[i, 1];
				array[i] = array2;
			}
			return array;
		}
		public static object GetARStructItemList(ArrayList items, bool useXml)
		{
			if (!useXml)
			{
				return ARTypeMgr.Instance.toVariant(items);
			}
			ArrayList given = new ArrayList(items);
			for (int i = 0; i < items.get_Count(); i++)
			{
				((StructureItem)items.get_Item(i)).Type = (((StructureItem)items.get_Item(i)).Type | 1073741824);
			}
			return ARTypeMgr.Instance.toVariant(given);
		}
		public static ArrayList BuildArrayListFromBulkEntryTxnReturnList(object[] retVals)
		{
			ArrayList arrayList = new ArrayList();
			if (retVals != null)
			{
				for (int i = 0; i < retVals.Length; i++)
				{
					object obj = retVals[i];
					object[] array = (object[])obj;
					if (array != null)
					{
						switch (Convert.ToInt32(array[0]))
						{
						case 1:
						{
							object arg_70_0 = array;
							int[] array2 = new int[2];
							array2[0] = 1;
							string entryId = (string)Util.GetAt(arg_70_0, array2);
							ArrayList statusList = ARTypeMgr.Instance.ArrayListFromVariant(typeof(Status), (object[])Util.GetAt(array, new int[]
							{
								1,
								1
							}), null);
							arrayList.Add(new BulkCreateEntryReturn(entryId, statusList));
							break;
						}
						case 2:
						{
							ARTypeMgr arg_F1_0 = ARTypeMgr.Instance;
							Type arg_F1_1 = typeof(Status);
							object arg_E6_0 = array;
							int[] array2 = new int[2];
							array2[0] = 1;
							ArrayList statusList2 = arg_F1_0.ArrayListFromVariant(arg_F1_1, (object[])Util.GetAt(arg_E6_0, array2), null);
							arrayList.Add(new BulkSetEntryReturn(statusList2));
							break;
						}
						case 3:
						{
							ARTypeMgr arg_135_0 = ARTypeMgr.Instance;
							Type arg_135_1 = typeof(Status);
							object arg_12A_0 = array;
							int[] array2 = new int[2];
							array2[0] = 1;
							ArrayList statusList3 = arg_135_0.ArrayListFromVariant(arg_135_1, (object[])Util.GetAt(arg_12A_0, array2), null);
							arrayList.Add(new BulkDeleteEntryReturn(statusList3));
							break;
						}
						case 4:
						{
							object arg_15F_0 = array;
							int[] array2 = new int[2];
							array2[0] = 1;
							string entryId2 = (string)Util.GetAt(arg_15F_0, array2);
							ArrayList statusList4 = ARTypeMgr.Instance.ArrayListFromVariant(typeof(Status), (object[])Util.GetAt(array, new int[]
							{
								1,
								1
							}), null);
							arrayList.Add(new BulkMergeEntryReturn(entryId2, statusList4));
							break;
						}
						case 5:
						{
							object arg_1C6_0 = array;
							int[] array2 = new int[2];
							array2[0] = 1;
							string outputDoc = (string)Util.GetAt(arg_1C6_0, array2);
							ArrayList statusList5 = ARTypeMgr.Instance.ArrayListFromVariant(typeof(Status), (object[])Util.GetAt(array, new int[]
							{
								1,
								1
							}), null);
							arrayList.Add(new BulkXmlCreateEntryReturn(outputDoc, statusList5));
							break;
						}
						case 6:
						{
							object arg_22D_0 = array;
							int[] array2 = new int[2];
							array2[0] = 1;
							string outputDoc2 = (string)Util.GetAt(arg_22D_0, array2);
							ArrayList statusList6 = ARTypeMgr.Instance.ArrayListFromVariant(typeof(Status), (object[])Util.GetAt(array, new int[]
							{
								1,
								1
							}), null);
							arrayList.Add(new BulkXmlSetEntryReturn(outputDoc2, statusList6));
							break;
						}
						case 7:
						{
							ARTypeMgr arg_2AB_0 = ARTypeMgr.Instance;
							Type arg_2AB_1 = typeof(Status);
							object arg_2A0_0 = array;
							int[] array2 = new int[2];
							array2[0] = 1;
							ArrayList statusList7 = arg_2AB_0.ArrayListFromVariant(arg_2AB_1, (object[])Util.GetAt(arg_2A0_0, array2), null);
							arrayList.Add(new BulkXmlDeleteEntryReturn(statusList7));
							break;
						}
						}
					}
				}
			}
			return arrayList;
		}
		public static int GetARType(object val)
		{
			if (val.GetType() == typeof(object[]))
			{
				return Convert.ToInt32(((object[])val)[0]);
			}
			if (val.GetType() == typeof(CurrencyValue))
			{
				return 12;
			}
			if (val.GetType() == typeof(ByteList))
			{
				return 9;
			}
			if (val.GetType() == typeof(Attachment))
			{
				return 11;
			}
			if (val.GetType() == typeof(DiaryList))
			{
				return 5;
			}
			if (val.GetType() == typeof(ArrayList) && ((ArrayList)val).get_Item(0) is Coordinate)
			{
				return 41;
			}
			if (val.GetType() == typeof(uint))
			{
				return 40;
			}
			if (val.GetType() == typeof(int) || val.GetType() == typeof(short))
			{
				return 2;
			}
			if (val.GetType() == typeof(string))
			{
				return 4;
			}
			if (val.GetType() == typeof(double))
			{
				return 3;
			}
			if (val.GetType() == typeof(decimal))
			{
				return 10;
			}
			if (val.GetType() == typeof(DateTime))
			{
				return 7;
			}
			if (val.GetType() == typeof(Keyword))
			{
				return 1;
			}
			return 0;
		}
		public static uint GetARActionType(Action action, bool isInActiveLink)
		{
			if (isInActiveLink)
			{
				if (action is RunMacroAction)
				{
					return 1u;
				}
				if (action is SetFieldsAction)
				{
					return 2u;
				}
				if (action is RunProcessAction)
				{
					return 3u;
				}
				if (action is MessageAction)
				{
					return 4u;
				}
				if (action is ChangeFieldAction)
				{
					return 5u;
				}
				if (action is DDEAction)
				{
					return 6u;
				}
				if (action is PushFieldsAction)
				{
					return 7u;
				}
				if (action is DirectSqlAction)
				{
					return 8u;
				}
				if (action is OleAutomationAction)
				{
					return 9u;
				}
				if (action is OpenWindowAction)
				{
					return 10u;
				}
				if (action is CommitChangesAction)
				{
					return 11u;
				}
				if (action is CloseWindowAction)
				{
					return 12u;
				}
				if (action is CallGuideAction)
				{
					return 13u;
				}
				if (action is ExitGuideAction)
				{
					return 14u;
				}
				if (action is GotoGuideLabelAction)
				{
					return 15u;
				}
				if (action is WaitAction)
				{
					return 16u;
				}
				if (action is GotoAction)
				{
					return 17u;
				}
				if (action is ServiceAction)
				{
					return 18u;
				}
				return 0u;
			}
			else
			{
				if (action is NotifyAction)
				{
					return 1u;
				}
				if (action is FilterMessageAction)
				{
					return 2u;
				}
				if (action is LogAction)
				{
					return 3u;
				}
				if (action is SetFieldsAction)
				{
					return 4u;
				}
				if (action is RunProcessAction)
				{
					return 5u;
				}
				if (action is PushFieldsAction)
				{
					return 6u;
				}
				if (action is DirectSqlAction)
				{
					return 7u;
				}
				if (action is GotoAction)
				{
					return 8u;
				}
				if (action is CallGuideAction)
				{
					return 9u;
				}
				if (action is ExitGuideAction)
				{
					return 10u;
				}
				if (action is GotoGuideLabelAction)
				{
					return 11u;
				}
				return 0u;
			}
		}
		public static object ConvertToARActionList(ArrayList actions, bool isInActiveLink, Server server)
		{
			object[] array = ARTypeMgr.Instance.toVariant(actions, server) as object[];
			if (array == null || array.Length <= 0)
			{
				return array;
			}
			int num = 0;
			object[] array2 = new object[array.Length];
			IEnumerator enumerator = actions.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					Action action = (Action)enumerator.get_Current();
					array2[num] = new object[]
					{
						Util.GetARActionType(action, isInActiveLink),
						(action is RunProcessAction) ? ((object[])array[num])[0] : array[num]
					};
					num++;
				}
			}
			finally
			{
				IDisposable disposable = enumerator as IDisposable;
				if (disposable != null)
				{
					disposable.Dispose();
				}
			}
			return array2;
		}
		public static object ConvertToARAssignmentList(ArrayList assignments, Server server, string toForm, uint fieldId, string fromForm)
		{
			object[] array = null;
			if (assignments != null && assignments.get_Count() > 0)
			{
				int num = 0;
				array = new object[assignments.get_Count()];
				IEnumerator enumerator = assignments.GetEnumerator();
				try
				{
					while (enumerator.MoveNext())
					{
						object current = enumerator.get_Current();
						array[num++] = Util.ConvertToARAssignment(current, server, toForm, fieldId, fromForm);
					}
				}
				finally
				{
					IDisposable disposable = enumerator as IDisposable;
					if (disposable != null)
					{
						disposable.Dispose();
					}
				}
			}
			return array;
		}
		public static object ConvertToARAssignment(object curAssignment, Server server, string toForm, uint fieldId, string fromForm)
		{
			object result = null;
			if (curAssignment is Array)
			{
				object obj = ((object[])curAssignment)[1];
				switch (Convert.ToUInt32(((object[])curAssignment)[0]))
				{
				case 0u:
					result = new object[]
					{
						0,
						Util.ConvertToARValue(new object[]
						{
							0,
							0
						})
					};
					break;
				case 1u:
					result = new object[]
					{
						1,
						Util.ConvertToARValue(obj)
					};
					break;
				case 2u:
				case 4u:
				case 5u:
					result = server.ParseAssignment(toForm, fieldId, fromForm, (string)obj);
					break;
				case 3u:
				{
					string text = obj as string;
					if (text != null)
					{
						if (text.StartsWith("$PROCESS$"))
						{
							text = text.Substring("$PROCESS$".get_Length());
						}
						text = text.TrimStart(new char[0]);
						result = new object[]
						{
							3,
							text
						};
					}
					else
					{
						result = new object[]
						{
							0,
							Util.ConvertToARValue(new object[]
							{
								0,
								0
							})
						};
					}
					break;
				}
				case 6u:
				{
					string text2 = obj as string;
					if (text2 != null)
					{
						if (text2.StartsWith("$DDE$"))
						{
							text2 = text2.Substring("$DDE$".get_Length());
						}
						text2 = text2.TrimStart(new char[0]);
						string[] array = text2.Split(";".ToCharArray());
						string text3 = "";
						string text4 = "";
						string text5 = "";
						string text6 = "";
						if (array != null && array.Length >= 0)
						{
							text3 = array[0];
						}
						if (array != null && array.Length >= 1)
						{
							text4 = array[1];
						}
						if (array != null && array.Length >= 2)
						{
							text5 = array[2];
						}
						if (array != null && array.Length >= 3)
						{
							text6 = array[3];
						}
						result = new object[]
						{
							6,
							new object[]
							{
								text3,
								text4,
								text6,
								3,
								text5,
								""
							}
						};
					}
					else
					{
						result = new object[]
						{
							0,
							Util.ConvertToARValue(new object[]
							{
								0,
								0
							})
						};
					}
					break;
				}
				case 7u:
				{
					string text7 = obj as string;
					uint num;
					if (text7 != null && text7.StartsWith("$") && text7.EndsWith("$"))
					{
						num = Convert.ToUInt32(text7.Substring(1, text7.get_Length() - 2));
					}
					else
					{
						num = Convert.ToUInt32(obj);
					}
					result = new object[]
					{
						7,
						new object[]
						{
							"",
							"",
							num,
							0,
							0
						}
					};
					break;
				}
				case 8u:
				{
					string text8 = obj as string;
					uint num2;
					if (text8 != null && text8.StartsWith("$") && text8.EndsWith("$"))
					{
						num2 = Convert.ToUInt32(text8.Substring(1, text8.get_Length() - 2));
					}
					else
					{
						num2 = Convert.ToUInt32(obj);
					}
					result = new object[]
					{
						8,
						new object[]
						{
							"",
							new object[0],
							num2
						}
					};
					break;
				}
				}
			}
			else
			{
				result = new object[]
				{
					1,
					Util.ConvertToARValue(curAssignment)
				};
			}
			return result;
		}
		public static object ConvertToARFieldAssignmentList(ArrayList fieldAssignments, Server server, string toForm, string fromForm, bool isByMatchingIds, bool isSetToDefaults)
		{
			object[] array = null;
			if (isByMatchingIds)
			{
				object[] array2 = new object[1];
				object[] arg_63_0 = array2;
				int arg_63_1 = 0;
				object[] array3 = new object[2];
				array3[0] = 98L;
				object[] arg_60_0 = array3;
				int arg_60_1 = 1;
				object[] array4 = new object[2];
				array4[0] = 2;
				object[] arg_5D_0 = array4;
				int arg_5D_1 = 1;
				object[] array5 = new object[7];
				array5[3] = 1;
				array5[4] = 98L;
				arg_5D_0[arg_5D_1] = array5;
				arg_60_0[arg_60_1] = array4;
				arg_63_0[arg_63_1] = array3;
				array = array2;
			}
			else
			{
				if (isSetToDefaults)
				{
					object[] array2 = new object[1];
					object[] arg_CD_0 = array2;
					int arg_CD_1 = 0;
					object[] array3 = new object[2];
					array3[0] = 97L;
					object[] arg_CA_0 = array3;
					int arg_CA_1 = 1;
					object[] array4 = new object[2];
					array4[0] = 2;
					object[] arg_C7_0 = array4;
					int arg_C7_1 = 1;
					object[] array5 = new object[7];
					array5[3] = 1;
					array5[4] = 97L;
					arg_C7_0[arg_C7_1] = array5;
					arg_CA_0[arg_CA_1] = array4;
					arg_CD_0[arg_CD_1] = array3;
					array = array2;
				}
				else
				{
					if (fieldAssignments != null && fieldAssignments.get_Count() > 0)
					{
						int num = 0;
						array = new object[fieldAssignments.get_Count()];
						IEnumerator enumerator = fieldAssignments.GetEnumerator();
						try
						{
							while (enumerator.MoveNext())
							{
								object current = enumerator.get_Current();
								uint num2 = Convert.ToUInt32(((object[])current)[0]);
								object obj = ((object[])current)[1];
								uint num3 = Convert.ToUInt32(((object[])current)[2]);
								object[] array6 = new object[]
								{
									num2,
									Util.ConvertToARAssignment(new object[]
									{
										num3,
										obj
									}, server, toForm, num2, fromForm)
								};
								array[num++] = array6;
							}
						}
						finally
						{
							IDisposable disposable = enumerator as IDisposable;
							if (disposable != null)
							{
								disposable.Dispose();
							}
						}
					}
				}
			}
			return array;
		}
		public static EntryListFieldList ConvertToEntryListFieldList(FieldIdList list)
		{
			EntryListFieldList entryListFieldList = new EntryListFieldList();
			IEnumerator enumerator = list.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					object current = enumerator.get_Current();
					entryListFieldList.AddField(Convert.ToInt32(current), 20, " ");
				}
			}
			finally
			{
				IDisposable disposable = enumerator as IDisposable;
				if (disposable != null)
				{
					disposable.Dispose();
				}
			}
			return entryListFieldList;
		}
		public static uint[] ConvertToUInt32Array(FieldIdList list)
		{
			uint[] array = new uint[list.get_Count()];
			int num = 0;
			IEnumerator enumerator = list.GetEnumerator();
			try
			{
				while (enumerator.MoveNext())
				{
					object current = enumerator.get_Current();
					array[num++] = Convert.ToUInt32(current);
				}
			}
			finally
			{
				IDisposable disposable = enumerator as IDisposable;
				if (disposable != null)
				{
					disposable.Dispose();
				}
			}
			return array;
		}
		public static void EnsureDirExists(string path)
		{
			if (Directory.Exists(path))
			{
				return;
			}
			Directory.CreateDirectory(path);
		}
		public static string EscapeFieldNameForARX(string name)
		{
			name.Replace("\\", "\\\\");
			name.Replace("\"", "\\\"");
			return name;
		}
		public static string QuoteStringForARX(string val)
		{
			val = val.Replace("\"", "\\\"");
			return "\"" + val + "\"";
		}
		public static string QuoteStringForCSV(string val)
		{
			val = val.Replace("\"", "\"\"");
			return "\"" + val + "\"";
		}
		public static bool CompareArrayLists(ArrayList arr1, ArrayList arr2)
		{
			if (arr1.get_Count() != arr2.get_Count())
			{
				return false;
			}
			for (int i = 0; i < arr1.get_Count(); i++)
			{
				if (!arr1.get_Item(i).Equals(arr2.get_Item(i)))
				{
					return false;
				}
			}
			return true;
		}
		public static decimal ToDecimal(object val)
		{
			try
			{
				decimal result = Convert.ToDecimal(val);
				return result;
			}
			catch
			{
			}
			CultureInfo cultureInfo = new CultureInfo("en-US", false);
			try
			{
				decimal result = decimal.Parse(val.ToString(), cultureInfo.get_NumberFormat());
				return result;
			}
			catch
			{
			}
			return 0m;
		}
		public static bool isStatusHistoryField(object id)
		{
			try
			{
				if (Convert.ToInt32(id) == 15)
				{
					bool result = true;
					return result;
				}
				if ("Status History".CompareTo(id.ToString()) == 0)
				{
					bool result = true;
					return result;
				}
			}
			catch (Exception)
			{
			}
			return false;
		}
		internal static string GetCombinedStringFromParts(string[] parts, int startIndex, char appendChar)
		{
			string text = "";
			if (parts == null || parts.Length <= startIndex)
			{
				return text;
			}
			int i = startIndex;
			int num = parts.Length;
			while (i < num)
			{
				if (i != startIndex)
				{
					text += appendChar;
				}
				text += parts[i];
				i++;
			}
			return text;
		}
	}
}
